﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCPets.Models;
using MVCPets.Data.Model;
using System.IO;

namespace MVCPets.Controllers
{
  public class PetsController : BaseController
  {
    #region Index
    [HttpGet]
    public ActionResult Index()
    {
      return View("Status", PetsModel.GetPetsByStatus("Available"));
    }
    #endregion

    #region Status
    [HttpGet]
    public ActionResult Status(string status)
    {
      var foo = PetsModel.GetPetsByStatus(status);

      if (foo == null || foo.Pets.Count == 0) return RedirectToAction("Index");
      else return View(foo);
    }
    #endregion

    #region Details
    [HttpGet]
    public ActionResult Details(Guid id)
    {
      return View(new PetsModel(id));
    }
    #endregion

		#region images
		[HttpPost]
		public ActionResult UploadImage(Guid PetID, HttpPostedFileBase FileBytes)
		{
			byte[] fBytes = new byte[FileBytes.ContentLength];
			using (Stream stream = FileBytes.InputStream)
			{
				stream.Read(fBytes, 0, FileBytes.ContentLength);
			}

			PetsModel.AddImage(PetID.ToString(), FileBytes.ContentType, fBytes);
			return View("Details", new PetsModel(PetID));
		}

		[HttpGet]
		public JsonResult Images(string PetID)
		{
			return Json(PetsModel.GetImageData(PetID), JsonRequestBehavior.AllowGet);
		}
		#endregion
	}
}
